# coding=utf-8

from flask import Blueprint, request, jsonify, json, make_response, redirect, g
from common.models.User import User
from common.libs.user.UserLibs import UserLibs
from common.libs.Helper import ops_render
from common.libs.UrlManager import UrlManager
from application import app,db
from web.controllers.BaseControllerHelper import *
from .Helper import *

route_user = Blueprint( 'user_page', __name__ )

@route_user.route("/login", methods=["GET", "POST"])
def login() :
    """
    用户登录方法，GET和POST的ajax请求公用
    :return:
    """
    #如果是GET请求，则跳转页面
    if request.method == "GET" :
        return ops_render("/user/login.html")

    resp = gene_response(msg="登录成功")
    req = request.values
    login_name= get_values_by_request(req, "login_name")
    login_pwd = get_values_by_request(req, "login_pwd")

    if login_name is None or len(login_name) < 1 :
        resp = gene_response(code=-1, msg="登录用户名格式不正确")
        return jsonify(resp)

    if login_pwd is None or len(login_pwd) < 1 :
        resp = gene_response(code=-1, msg="登录密码格式不正确")
        return jsonify(resp)

    user_info = User.query.filter_by( login_name=login_name ).first()

    if not user_info :
        resp = gene_response(code=-1, msg="请输入正确的用户名和密码")
        return jsonify(resp)

    if user_info.login_pwd != UserLibs.genePassword(login_pwd, user_info.login_salt ):
        resp = gene_response(code=-1, msg="请输入正确的用户名和密码")
        return jsonify(resp)

    if user_info.status != 1:
        resp = gene_response(code=-1, msg="账号状态异常")
        return jsonify(resp)

    response = make_response( json.dumps( resp ) )
    response.set_cookie(app.config["AUTH_COOKIE_NAME"], "%s#%s" % (UserLibs.geneAuthCode(user_info), user_info.uid) )
    return response


@route_user.route("/edit", methods=["GET", "POST"])
def edit() :
    # 如果是GET请求，则跳转页面
    if request.method == "GET":
        return ops_render("/user/edit.html")

    resp = gene_response(msg="修改成功")
    req = request.values
    nickname = get_values_by_request(req, "nickname")
    email = get_values_by_request(req, "email")

    if nickname is None or len(nickname) < 1:
        resp = gene_response(code=-1, msg="请输入符合规范的姓名")
        return jsonify(resp)

    if email is None or len(email) < 1:
        resp = gene_response(code=-1, msg="请输入符合规范的邮箱")
        return jsonify(resp)

    user_info = g.current_user
    user_info.nickname = nickname
    user_info.email = email

    db.session.add(user_info)
    db.session.commit()
    return jsonify(resp)


@route_user.route("/reset_pwd", methods=["GET", "POST"])
def reset_pwd() :
    # 如果是GET请求，则跳转页面
    if request.method == "GET":
        return ops_render("/user/reset_pwd.html")

    resp = gene_response(msg="修改成功")
    req = request.values
    old_password = get_values_by_request(req, "old_password")
    new_password = get_values_by_request(req, "new_password")
    user_info = g.current_user

    if old_password is None or len(old_password) < 6:
        resp = gene_response(code=-1, msg="请输入符合规范的原密码")
        return jsonify(resp)

    if new_password is None or len(new_password) < 6:
        resp = gene_response(code=-1, msg="请输入符合规范的新密码")
        return jsonify(resp)

    if UserLibs.genePassword(old_password, user_info.login_salt) != user_info.login_pwd :
        resp = gene_response(code=-1, msg="原始密码有误，重新输入哦")
        return jsonify(resp)

    if old_password == new_password:
        resp = gene_response(code=-1, msg="请重新输入一个吧，新密码和原密码不能相同哦")
        return jsonify(resp)

    if user_info.uid == 1:
        resp = gene_response(code=-1, msg="该用户是演示账号，不准修改密码和登录用户名")
        return jsonify(resp)

    user_info.login_pwd = UserLibs.genePassword(new_password, user_info.login_salt)

    db.session.add(user_info)
    db.session.commit()

    #修改密码之后cookie值需要更换
    response = make_response(json.dumps(resp))
    response.set_cookie(app.config["AUTH_COOKIE_NAME"], "%s#%s" % (UserLibs.geneAuthCode(user_info), user_info.uid))
    return response

@route_user.route("/logout")
def logout() :
    response = make_response( redirect( UrlManager.buildUrl("/user/login") ) )
    response.delete_cookie( app.config["AUTH_COOKIE_NAME"] )
    return response

